package com.example.zjhuojback.service;

import cn.hutool.core.date.DateTime;
import com.example.zjhuojback.entity.User;
import com.example.zjhuojback.exception.ServiceException;
import com.example.zjhuojback.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    public void insertUser(User user) {
        userMapper.insert(user);
    }

    public void updataUser(User user) {
        userMapper.updateUser(user);
    }
    public void updateUserPermission(String username, int permission) {
        if (Objects.equals(username, "admin")) userMapper.updateUserPermissionAll(permission);
        else userMapper.updateUserPermission(username, permission);
    }

    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    public void resetPossword(Integer id) {
        userMapper.resetPossword(id);
    }

    public void betchDeleteUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);
        }
    }

    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        // 在返回的用户列表中隐藏密码
        for (User user : users) {
            user.setPassword("*********");
        }
        return users;
    }

    public User selectById(Integer id) {
        User user = userMapper.selectById(id);
        // 隐藏密码
        if (user != null) {
            user.setPassword("*********");
        }
        return user;
    }

    /**
     * 验证账号是否存在并且合法
     * @return
     */
    public User login(User user) {
        // 用用户名查询信息
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser == null) {
            // 抛出一个自定义异常
            throw new ServiceException("账号不存在");
        }
        if (!dbUser.getPassword().equals(user.getPassword())) {
            throw new ServiceException("密码错误");
        }
        updateLoginInfo(dbUser.getId());
        // 隐藏密码并返回
        dbUser.setPassword("*********");
        return dbUser;
    }

    @Autowired
    private HttpServletRequest request;

    // 用户登录后更新登录信息
    public void updateLoginInfo(Integer id) {
        String ip = getClientIp(); // 获取客户端 IP 地址
        LocalDateTime loginTime = LocalDateTime.now(); // 获取当前时间作为登录时间

        // 假设你已经定义了一个方法更新用户的登录时间和IP
        userMapper.updateLoginTimeAndIp(id, loginTime, ip);
    }

    // 获取客户端 IP 地址
    private String getClientIp() {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.isEmpty()) {
            ip = request.getRemoteAddr();
        } else {
            ip = ip.split(",")[0];
        }
        return ip;
    }

    public User register(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser != null){
            throw new ServiceException("用户名已存在");
        }
        if (user.getPermission() == null) {
            user.setPermission("0");
        }
        userMapper.insert(user);
        return null;
    }

    // 用户过题数量增加
    public void updateSubmit(Integer first, boolean ac, String username) {
        if (first == 1 && ac) { userMapper.updateAccept(username); }
        else { userMapper.updateSubmit(username); }
    }
}
